package utils

import (
	"fmt"
	"reflect"
)

// ConvertMapToSlice 将 map 转换为切片
func ConvertMapToSlice(idMap map[int64]struct{}) []int64 {
	idSlice := make([]int64, 0, len(idMap))
	for id := range idMap {
		idSlice = append(idSlice, id)
	}
	return idSlice
}

// StructToMap 将结构体转换为map[string]interface{}
func StructToMap(obj interface{}) map[string]interface{} {
	result := make(map[string]interface{})
	if obj == nil {
		return result
	}

	val := reflect.ValueOf(obj)
	if val.Kind() == reflect.Ptr {
		val = val.Elem()
	}

	for i := 0; i < val.NumField(); i++ {
		field := val.Field(i)
		name := val.Type().Field(i).Name
		result[name] = field.Interface()
	}

	return result
}

func PrintR(data interface{}) {
	val := reflect.ValueOf(data)

	if val.Kind() != reflect.Slice {
		fmt.Printf("Unsupported type: %T, expected a slice\n", data)
		return
	}

	for i := 0; i < val.Len(); i++ {
		item := val.Index(i).Interface()
		fmt.Println("PrintR:", StructToMap(item))
	}
}
